//
//  ByBlueToothMeaturesS3Model.m
//  laserdevice_ios
//
//  Created by pro on 16/12/12.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByBlueToothMeaturesS3Model.h"

@implementation ByBlueToothMeaturesS3Model

- (instancetype)initWithDataHexString:(NSString *)hexStr {
    
    if (self = [super initWithDataHexString:hexStr]) {
        
        self.name = [NSString stringWithFormat:@"S3%@", STRING(@"moxing")];

        NSString *lengthStr1 = [hexStr substringWithRange:NSMakeRange(42, 18)];
        self.topLength = [[self getNumberFromHexStr:lengthStr1] floatValue];
        NSString *lengthStr2 = [hexStr substringWithRange:NSMakeRange(70, 18)];
        self.height = [[self getNumberFromHexStr:lengthStr2] floatValue];
        NSString *lengthStr3 = [hexStr substringWithRange:NSMakeRange(98, 18)];
        self.bottomLength = [[self getNumberFromHexStr:lengthStr3] floatValue];
        NSString *areaStr = [hexStr substringWithRange:NSMakeRange(126, 18)];
        self.area = [[self getNumberFromHexStr:areaStr] floatValue];
        
        NSLog(@"%li,%.2f,%.2f,%.2f,%.2f", self.numOfData, self.topLength, self.height, self.bottomLength, self.area);
    }
    return self;
}

- (instancetype)initWithJsonDic:(NSDictionary *)jsonDic {
    
    if (self = [super initWithJsonDic:jsonDic]) {
        
        NSArray *attributes = jsonDic[@"attributes"];
        self.topLength = [attributes[0] floatValue];
        self.height = [attributes[1] floatValue];
        self.bottomLength = [attributes[2] floatValue];
        self.area = [attributes[3] floatValue];
    }
    return self;
}


- (NSDictionary *)saveDic {
    
    NSArray *attributes = @[[NSString stringWithFormat:@"%.6lf", self.topLength],
                            [NSString stringWithFormat:@"%.6lf", self.height],
                            [NSString stringWithFormat:@"%.6lf", self.bottomLength],
                            [NSString stringWithFormat:@"%.6lf", self.area],];
    return @{@"name"        :self.name,
             @"attributes"  :attributes,};
}

- (NSString *)topLengthStr {
    
    return [self getStringFromLength:self.topLength];
}
- (NSString *)heightStr {
    
    return [self getStringFromLength:self.height];
}
- (NSString *)bottomLengthStr {
    
    return [self getStringFromLength:self.bottomLength];
}
- (NSString *)areaStr {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    
    NSString *ts, *bs, *hs;
    switch (unit) {
        case 0: // 米
            ts = [NSString stringWithFormat:@"%.3lf", _topLength];
            bs = [NSString stringWithFormat:@"%.3lf", _bottomLength];
            hs = [NSString stringWithFormat:@"%.3lf", _height];
            break;
        case 1: // 英尺
        case 2: // 英寸
        case 3: // 英尺+英寸
            ts = [NSString stringWithFormat:@"%.3lf", _topLength * 3.2808399];
            bs = [NSString stringWithFormat:@"%.3lf", _bottomLength * 3.2808399];
            hs = [NSString stringWithFormat:@"%.3lf", _height * 3.2808399];
            break;
        default:
            break;
    }
    
    _area = (ts.floatValue + bs.floatValue) * hs.floatValue / 2;
    return [NSString stringWithFormat:@"%.3f", _area];
}


@end
